package servlet.initparam;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import member.domain.Member;

/**
 * Servlet implementation class GreetingServlet
 */
public class GreetingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public GreetingServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		application scope에서 admin 이름으로 biding된 속성 조회
		ServletContext ctx = getServletContext();
		Member admin = (Member)ctx.getAttribute("admin");
		System.out.println("GreetingServlet admin : " + admin);
		
		//요청 파라미터 조회
		//1. 요청한 정보에 대한 한글 처리
		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("name");
		//2. 초기 파라미터 조회
		ServletConfig conf = getServletConfig();
		String greeting1 = conf.getInitParameter("greeting");
		String greeting2 = getInitParameter("greeting2");
		
		ServletContext context = getServletContext();
		String adminEmail = context.getInitParameter("admin email");
		String companyAddress = context.getInitParameter("company address");
		
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("<html><body>");
		out.println(name+"님"+greeting1+greeting2);
		out.println("<hr>관리자 이메일 : " + adminEmail + ", 회사 주소 : " + companyAddress);
		out.println("</body></html>");
		
	}

}
